{include file="public/header" /}

<body>
	<div class="layui-fluid">
		<div class="layui-card">
			<div class="layui-form layui-card-header layuiadmin-card-header-auto">
				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">菜单名称</label>
						<div class="layui-input-block">
							<select name="ID">
								<!--<option value="0">顶级菜单</option>-->
								{foreach $data as $vo}
								<option value=" {$vo.ID}">{$vo._Name}</option>
								{/foreach}
							</select>
						</div>
					</div>
					<div class="layui-inline">
						<button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
							<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
						</button>
					</div>
				</div>
			</div>

			<div class="layui-card-body">
				<div style="padding-bottom: 10px;">
					<!--<button class="layui-btn layuiadmin-btn-admin" data-type="batchdel">删除</button>-->
					<button class="layui-btn layuiadmin-btn-admin" data-type="add">添加</button>
				</div>
				<table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
				<script type="text/html" id="buttonTpl">
						{{# if(d.check == true){ }}
						<button class="layui-btn layui-btn-xs">已审核</button> {{# } else { }}
						<button class="layui-btn layui-btn-primary layui-btn-xs">未审核</button> {{# } }}
					</script>
				<script type="text/html" id="table-useradmin-webuser">
						<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon"></i>编辑</a>
						<!--{{# if(d.role == '超级管理员'){ }}
						<a class="layui-btn layui-btn-disabled layui-btn-xs"><i class="layui-icon layui-icon-delete"></i>删除</a>
						{{# } else { }}
						<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
						{{# } }}-->
					</script>
			</div>
		</div>
	</div>
</body>

{include file="public/footer" /}
<script>
	layui.use(['index', 'table'], function () {
		var $ = layui.$,
			form = layui.form,
			table = layui.table;

		//监听搜索
		form.on('submit(LAY-user-back-search)', function (data) {
			var field = data.field;
			//执行重载
			table.reload('LAY-user-manage', {
				where: field
			});
		});

		//监听工具条
		table.on('tool(LAY-user-manage)', function (obj) {
			var data = obj.data;
			if (obj.event === 'detail') {
				layer.msg('ID：' + data.id + ' 的查看操作');
			}else if (obj.event === 'edit') {
				layer.open({
					type: 2,
					title: '编辑菜单',
					content: 'editss.html?ID=' + data.ID,
					area: ['80%', '80%'],
					btn: ['确定', '取消'],
					yes: function (index, layero) {
						var iframeWindow = window['layui-layer-iframe' + index],
							submitID = 'LAY-user-back-submit',
							submit = layero.find('iframe').contents().find('#' + submitID);
						//监听提交
						iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
							var field = data.field; //获取提交的字段
							$.ajax({
								'type': 'post',
								'url': "{:url('admin/menu/edit')}",
								'data': field,
								dataType: 'json',
								success: function (res) {
									if (res.code == '0') {
										layer.msg(res.msg, {
											icon: 1
										});
									} else {
										layer.msg(res.msg, {
											icon: 5
										});
									}
									table.reload('LAY-user-manage');
									layer.close(index); //关闭弹层
								}
							});

						});

						submit.trigger('click');
					}
				});

			}
		});

		table.render({
			elem: '#LAY-user-manage',
			url: "{:url('admin/menu/get_list')}",
			toolbar: '#test-table-toolbar-toolbarDemo',
			title: '菜单数据表',
			cols:
				[[//标题栏
					{ type: 'checkbox', fixed: 'left' }
					, { type: 'numbers', title: '排序', width: 40, fixed: 'left', unresize: true }
					, { field: 'Name', title: '名称', edit: 'text', width: 110, rowspan: 2, fixed: 'left' } //rowspan即纵向跨越的单元格数
					, { field: 'Url', edit: 'text', title: 'URL' }
					, { field: 'CreateTime', width: 200, edit: 'text', title: '加入时间' }
					, { field: 'UpdateTime', width: 200, edit: 'text', title: '修改时间' }
					, { fixed: 'right', width: 70, edit: 'text', title: '操作', toolbar: '#table-useradmin-webuser' }
				]],
			page: true
		});

		//事件
		var active = {
			add: function () {
				layer.open({
					type: 2,
					title: '添加菜单',
					content: 'adminform.html',
					area: ['80%', '80%'],
					btn: ['确定', '取消'],
					yes: function (index, layero) {
						var iframeWindow = window['layui-layer-iframe' + index],
							submitID = 'LAY-user-back-submit',
							submit = layero.find('iframe').contents().find('#' + submitID);
						//监听提交
						iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
							var field = data.field; //获取提交的字段
							//提交 Ajax 成功后，静态更新表格中的数据
							$.ajax({
								'type': 'post',
								'url': "{:url('admin/menu/add')}",
								'data': field,
								dataType: 'json',
								success: function (res) {
									if (res.code == '0') {
										layer.msg(res.msg, {
											icon: 1
										});
									} else {
										layer.msg(res.msg, {
											icon: 5
										});
									}

									table.reload('LAY-user-manage');
									layer.close(index); //关闭弹层
								}
							});
						});

						submit.trigger('click');
					}
				});
			}
		}
		$('.layui-btn.layuiadmin-btn-admin').on('click', function () {
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});
	});
</script>